package com.chengqs.rapid.disruptor.quicklystart;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <B>主类名称：</B>Main<BR>
 * <B>概要说明：</B>第一个ds小程序<BR>
 *
 * @author: chengqs
 * @create: 2025-03-11 15:44:15
 **/
public class DSMain {

    private static AtomicInteger count = new AtomicInteger(0);
    public static void main(String[] args) {

        int ringBufferSize = 8;
        OrderEventFactory orderEventFactory = new OrderEventFactory();
        Disruptor<OrderEvent> disruptor = new Disruptor<>(
                orderEventFactory,
                ringBufferSize,
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("ds-thread-" + count.getAndIncrement());
                        return thread;
                    }
                },
                ProducerType.SINGLE,
                new BlockingWaitStrategy()
        );

        disruptor.handleEventsWith(new OrderEventHandler());

        disruptor.start();

        RingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();

        OrderEventProducer producer = new OrderEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);

        for(long i = 0 ; i < 100; i++) {
            bb.putLong(0, i);
            producer.putData(bb);
        }

        disruptor.shutdown();
    }
}
